<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');

//Is a new category requested?
$newstatus=null;
if(isset($_POST['newCatName']) && $_POST['newCatName']!='') {
	if($_p->add_category(fetch::unescapePostVar($_POST['newCatName'])))
		$newstatus=true;
	else
		$newstatus=false;
}

//Is a delete requested?
$delstatus=null; //null-nothing, true-pass, false-fail
if(is_numeric($_GET['delc'])&&$_p->tb_categories->idExists($_GET['delc'])) {
	if($_p->delete_category($_GET['delc']))
		$delstatus=true;
	else
		$delstatus=false;
}
else if(isset($_GET['delc'])) {
	$delstatus=false;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Categories</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="categories.php" class="breadcrumb">Manage Categories</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
		<h2>Manage Categories</h2>
		<?php
			//New category status
			
			//Deleting category status
			if(isset($_GET['del']) && $_p->category_id_exists($_GET['del'])) {
				echo('Confirm deleting the category "'.htmlentities($_p->cat_name($_GET['del']),ENT_QUOTES,'UTF-8').'"?<br />
				<a href="categories.php?delc='.$_GET['del'].'">Confirm</a> | <a href="categories.php">Cancel</a>');
			}
			if($delstatus===false)
				echo('An error occured when trying to delete the category (does it still exist?)');
			if($newstatus===false)
				echo('An error occured when trying to create the category (does it already exist?)');
		?>
		<h3>New Category</h3>
		<form action="categories.php" method="post">
			Name:<br />
			<input type="text" name="newCatName" /><br />
			<br />
			<input type="submit" value="Create Category" />
		</form>
		<h3>Current Categories</h3>
		<?php
			//Category list
			$categories=$_p->get_categories();
			if(count($categories)==0)
				echo('No categories to display...');
			foreach($categories as $catID=>$catInfo) {
				echo($catID.' - '.$catInfo['name']);
				if($catID!=0)
					echo(' <a href="categories.php?del='.$catID.'">[Delete]</a>');
				
				echo('<br />');
			}
		?>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>